package com.xhui.vimarket.zkplus.zul.attr.evt.multi;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zul.Listitem;

import com.xhui.vimarket.zkplus.zul.attr.AttrExt;
import com.xhui.vimarket.zkplus.zul.attr.bean.AttrTmpl;
import com.xhui.vimarket.zkplus.zul.attr.evt.AttrBaseComposer;
import com.xhui.vimarket.zkplus.zul.attr.mod.AttrCompUtil;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrMultiSubComp;

public class AddBtnComposer extends AttrBaseComposer {
    
    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = 4579587521568091904L;
    
    @Override
    public ComponentInfo doBeforeCompose(final Page page, final Component comp,
        final ComponentInfo info) {
        return info;
    }
    
    @Override
    public void doAfterCompose(final Component comp) throws Exception {
        super.doAfterCompose(comp);
        // 初始化
        this.init(comp);
    }
    
    /**
     * init.
     * @param comp Component
     */
    public void init(final Component comp) {
    }
    
    /**
     * onClick.
     * 
     */
    public void onClick() {
        // 控件根元素
        final Component rootComp = AttrMultiSubComp.getRootCompByAddOrDelBtn(this.self);
        // 找到上级Listitem
        final Listitem item = AttrMultiSubComp.getListitemByAddOrDelBtn(this.self);
        // Idx
        final long multiIdx = AttrCompUtil.getAttrCompIdx(rootComp);
        if (multiIdx < 0) {
            throw new RtManagerException("多实例控件，获取模板数据错误", this.getClass(), "onClick",
                "multiAttrTmplIdxNull");
        }
        // 获取控件
        final AttrExt attrExt = AttrCompUtil.getAttrExtComp(this.self);
        if (attrExt == null) {
            throw new RtManagerException("多实例控件，获取控件根错误", this.getClass(), "onClick",
                "multiAttrRootCompNull");
        }
        // 获取模板定义
        final AttrTmpl mltiAttrTmpl = AttrCompUtil.getEqualAttrTmpl(attrExt.getAttrSpec()
            .getAttrTmpl(), rootComp);
        if (mltiAttrTmpl == null) {
            throw new RtManagerException("多实例控件，获取模板数据为空", this.getClass(), "onClick",
                "multiAttrTmplNull");
        }
        // 调用增加实例方法，传入本行控件根节点
        AttrMultiSubComp.instance().doAddLine(attrExt, mltiAttrTmpl, item);
        // AttrMultiSubComp.fireChangeVal(rootComp);
    }
}
